package com.laoniu.biz.goods;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.laoniu.dto.PageResponse;
import com.laoniu.dto.response.GoodsStockDto;
import com.laoniu.dto.request.GoodsStockRequestDto;
import com.laoniu.entity.BaseGoods;
import com.laoniu.entity.BaseSupplier;
import com.laoniu.entity.GoodsStock;
import com.laoniu.entity.SecurityUser;
import com.laoniu.service.base.BaseGoodsService;
import com.laoniu.service.base.BaseSupplierService;
import com.laoniu.service.goods.GoodsStockService;
import com.laoniu.utils.SpringSecurityUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class GoodsStockBiz {

    @Autowired
    private GoodsStockService goodsStockService;
    @Autowired
    private BaseGoodsService goodsService;
    @Autowired
    private BaseSupplierService supplierService;

    public PageResponse<GoodsStockDto> pageList(GoodsStockRequestDto requestDto) {
        Page<GoodsStock> page = goodsStockService.listPage(requestDto);
        PageResponse<GoodsStockDto> result = new PageResponse<GoodsStockDto>();
        result.setPageNo(requestDto.getPageNo());
        result.setPageSize(requestDto.getPageSize());
        result.setTotal(page.getTotal());
        if (CollectionUtils.isNotEmpty(page.getRecords())){
            Map<Long,String> supplierMap = Maps.newHashMap();
            Map<Long,String> goodsMap = Maps.newHashMap();

            List<GoodsStockDto> dtos = Lists.newArrayList();
            for (GoodsStock record : page.getRecords()) {
                GoodsStockDto dto = Convert.convert(GoodsStockDto.class, record);
                //查询供应商名称
                if (supplierMap.containsKey(record.getSupplierId())){
                    dto.setSupplierName(supplierMap.get(record.getSupplierId()));
                }else {
                    BaseSupplier supplier = supplierService.getById(record.getSupplierId());
                    dto.setSupplierName(supplier.getName());
                    supplierMap.put(supplier.getId(), supplier.getName());
                }
                //查询商品名称
                if (goodsMap.containsKey(record.getGoodsId())){
                    dto.setGoodsName(goodsMap.get(record.getGoodsId()));
                }else {
                    BaseGoods goods = goodsService.getById(record.getGoodsId());
                    dto.setGoodsName(goods.getName());
                    goodsMap.put(goods.getId(), goods.getName());
                }
                dtos.add(dto);
            }
            result.setData(dtos);
        }
        return result;
    }

    /**
     * 增加商品进货
     * @param dto
     */
    public void addGoodsStock(GoodsStockDto dto) {
        SecurityUser user = SpringSecurityUtils.getUser();
        GoodsStock goodsStock = Convert.convert(GoodsStock.class, dto);
        goodsStock.setSumPrice(dto.getPrice().multiply(new BigDecimal(dto.getNum())));
        goodsStock.setOpTime(LocalDateTime.now());
        goodsStock.setOpId(user.getId());
        goodsStock.setOpName(user.getName());
        goodsStockService.save(goodsStock);
        goodsService.addNum(dto.getGoodsId(),dto.getNum());
    }

}
